using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BusinessEntities;

namespace RevRaider
{
    public partial class Form1 : Form
    {
        private Raiders _allRaiders;

        public Form1()
        {
            InitializeComponent();

            RetrieveArmoryRaidersProcess.DoWork += new DoWorkEventHandler(RetrieveAllRaiders);
            RetrieveArmoryRaidersProcess.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
        }

        private void GetRaidersCommand_Click(object sender, EventArgs e)
        {
            if (RetrieveArmoryRaidersProcess.IsBusy == false)
            {
                ProcessingLabel.Visible = true;
                RetrieveArmoryRaidersProcess.RunWorkerAsync();
            }
            else
            {
                ProcessingLabel.Text = "Have patience young padawen!";
            }
        }

        private void RetrieveAllRaiders(object sender, DoWorkEventArgs e)
        {
            Armory armory = new Armory();
            _allRaiders = armory.GetRaiders(new SearchCriteria());

        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // First, handle the case where an exception was thrown.
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else if (e.Cancelled)
            {
                // Next, handle the case where the user canceled 
                // the operation.
                // Note that due to a race condition in 
                // the DoWork event handler, the Cancelled
                // flag may not have been set, even though
                // CancelAsync was called.
                resultLabel.Text = "Canceled";
            }
            else
            {
                ProcessingLabel.Text = "Processing...";
                ProcessingLabel.Visible = false;

                if (_allRaiders != null)
                {
                    PopulateData();
                }
                else
                {
                    TotalRaidersField.Text = "0";
                }
            }
        }

        private void RemoveRaiderStrip_Click(object sender, EventArgs e)
        {
            bool dataChanged = false;

            foreach (Raider raider in ArmoryRaidersList.SelectedItems)
            {
                _allRaiders.Remove(raider);
                dataChanged = true;
            }

            if (dataChanged == true)
            {
                PopulateData();
            }
        }

        private void PopulateData()
        {
            _allRaiders.Sort(new Raider());

            ArmoryRaidersList.DataSource = null;
            ArmoryRaidersList.DataSource = _allRaiders;
            ArmoryRaidersList.DisplayMember = "Name";
                        
            TotalRaidersField.Text = _allRaiders.Count.ToString();
            WarriorCountField.Text = _allRaiders.WarriorCount.ToString();
            PaladinCountField.Text = _allRaiders.PaladinCount.ToString();
            HunterCountField.Text = _allRaiders.HunterCount.ToString();
            RogueCountField.Text = _allRaiders.RogueCount.ToString();
            PriestCountField.Text = _allRaiders.PriestCount.ToString();
            ShamanCountField.Text = _allRaiders.ShamanCount.ToString();
            MageCountField.Text = _allRaiders.MageCount.ToString();
            WarlockCountField.Text = _allRaiders.WarlockCount.ToString();
            DruidCountField.Text = _allRaiders.DruidCount.ToString();
        }

        private void ArmoryRaidersList_DrawItem(object sender, DrawItemEventArgs e)
        {

            if (e.Index == -1)
                return;
            //
            // Draw the background of the ListBox control for each item.
            // Create a new Brush and initialize to a Black colored brush
            // by default.
            //
            e.DrawBackground();
            Brush myBrush = Brushes.Black;
          
            // Determine the color of the brush to draw each item based class 
            switch (((Raider)((ListBox)sender).Items[e.Index]).Class)
            {                
                case RaiderClass.Warrior:
                    myBrush = Brushes.Brown;
                    break;
                case RaiderClass.Paladin:
                    myBrush = Brushes.Pink;
                    break;
                case RaiderClass.Hunter:
                    myBrush = Brushes.Green;
                    break;
                case RaiderClass.Rogue:
                    myBrush = Brushes.Yellow;
                    break;
                case RaiderClass.Priest:
                    myBrush = Brushes.White;
                    break;
                case RaiderClass.Shaman:
                    myBrush = Brushes.DarkBlue;
                    break;
                case RaiderClass.Mage:
                    myBrush = Brushes.LightBlue;
                    break;
                case RaiderClass.Warlock:
                    myBrush = Brushes.Violet;
                    break;
                case RaiderClass.Druid:
                    myBrush = Brushes.Orange;
                    break;
                default:
                    break;
            }
            
            // Draw the current item text based on the current 
            // Font and the custom brush settings.
            e.Graphics.DrawString(((Raider)((ListBox)sender).Items[e.Index]).Name, e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
        }
    }
}